﻿using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using InfoJetSoft.Service.Entity;
using System.Text.RegularExpressions;

namespace InfoJetSoft.Service.Util
{

    public class ExtXdUtilFunction : IExtensionFunctions
	{
		private Form form = null;

		public ExtXdUtilFunction( Form frm ){
			this.form = frm;
        }

        //CID 0755 //2011.01.14 //Begin
        #region IExtensionFunction Members

        public string Prefix
        {
            get { return this.form.Manifest.NsResolver.GetPrefix(this.NamespaceURI); }
        }

        public string NamespaceURI
        {
            get { return JetConstant.URI_XDUTIL; }
        }

        public IXsltContextFunction ResolveFunction(string prefix, string name, XPathResultType[] argTypes)
        {
            //CID 0931 //2011.05.29 //Begin
            if (name.Equals("Match"))
            {
                return new XPathFunction(this.form, prefix, name, 2, 2, argTypes, XPathResultType.Number);
            }
            else
            {
                throw new InfoJetException("Unimplemented Function.", prefix + ":" + name);
            }
            //CID 0931 //2011.05.29 //End
        }

        public object InvokeFunction(string prefix, string name, XsltContext xsltContext, object[] args, XPathNavigator docContext)
        {
            //CID 0931 //2011.05.29 //Begin
            if (name.Equals("Match"))
            {
                return this.Match(args[0] as string, args[1] as string);
            }
            else
            {
                throw new InfoJetException("Unimplemented Function.", prefix + ":" + name);
            }
            //CID 0931 //2011.05.29 //End
        }

        #endregion
        //CID 0755 //2011.01.14 //End

		public bool Match( string value, string pattern )
        {
            //CID 0756 //2011.01.07 //Begin
            pattern = "^" + pattern;
            //CID 0756 //2011.01.07 //End
			//CID 0111 //2008.09.09 //Begin
			MatchCollection matchCollection = Regex.Matches( value, pattern );
			if( matchCollection.Count == 1 )
			{
				//xdUtil:Match必须只匹配一次.
				//CID 0146 //2008.10.28 //Begin
				Match firstMatch = matchCollection[0];
				if( firstMatch.Index == 0 && firstMatch.Length == value.Length )
				{
					return true;
				}
				else
				{
					return false;
				}
				//CID 0146 //2008.10.28 //End
			}
			else
			{
				return false;
			}
			//CID 0111 //2008.09.09 //End
		}

        //CID 1007 //2011.08.16 //Begin
        public object FilterDouble(object value)
        {
            if (value is double)
            {
                double doubleValue = (double)value;
                return FloatingPointString.ToString(doubleValue);
            }
            else
            {
                return value;
            }
        }
        //CID 1007 //2011.08.16 //End

		//自定义的Util扩展函数，用于通过JetContext上的CustomExtFunction验证Node值。
		public bool InfoJetServiceCustomNodeValidation( string functionName, Object current )
		{
			if( current is XPathNodeIterator) 
			{
				XPathNodeIterator nodeIterator = (XPathNodeIterator) current;
				if(nodeIterator.MoveNext()) 
				{
					XmlNode currentNode = ((System.Xml.IHasXmlNode)nodeIterator.Current).GetNode();
					if( form.JetContext.CustomExtFunction != null )
					{
						object result = form.JetContext.CustomExtFunction.RunCustomExtFunction( form, functionName, currentNode );
						if( result is bool )
						{
							return (bool)result;
						}
					}
				}
			}
			return false;
		}
    }
}